package com.brokendust.client.task;

import com.brokendust.client.api.gui.Menu;
import com.brokendust.client.config.gui.MenuJson;
import com.brokendust.client.config.module.button.ClickButtonJson;
import com.brokendust.client.config.module.image.ImageShowJson;
import com.brokendust.client.config.module.item.ItemShowJson;
import com.brokendust.client.config.module.pullpanel.PullPanelJson;
import com.brokendust.client.config.module.text.TextLabelJson;
import com.brokendust.client.config.module.textfield.TextFieldJson;
import com.brokendust.client.manager.ClientManager;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.StringTextComponent;

public class GUITask {
    public static void upDataMenu(String jsonString) {
        if (!ClientManager.open) {
            return;
        }
        MenuJson menuJson = MenuJson.readJSON(jsonString);
        menuJson.refreshSize();
        for (String type : menuJson.getObject_list().keySet()) {
            String value = menuJson.getObject_list().get(type);
            if (type.startsWith("Button")) {
                ClickButtonJson clickButtonJson = ClickButtonJson.readJSON(value);
                clickButtonJson.refreshSize();
                String object_id = clickButtonJson.getObject_name();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(clickButtonJson);
            } else if (type.startsWith("TextField")) {
                TextFieldJson textFieldJson = TextFieldJson.readJSON(value);
                textFieldJson.refreshSize();
                String object_id = textFieldJson.getObject_name();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(textFieldJson);
            } else if (type.startsWith("Image")) {
                ImageShowJson imageShowJson = ImageShowJson.readJSON(value);
                imageShowJson.refreshSize();
                String object_id = imageShowJson.getObject_name();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(imageShowJson);
            } else if (type.startsWith("Text")) {
                TextLabelJson textLabelJson = TextLabelJson.readJSON(value);
                String object_id = textLabelJson.getObject_name();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(textLabelJson);
            } else if (type.startsWith("Item")) {
                ItemShowJson itemShowJson = ItemShowJson.readJSON(value);
                String object_id = itemShowJson.getObject_name();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(itemShowJson);
            } else {
                if (!type.startsWith("PullPanel")) {
                    continue;
                }
                PullPanelJson pullPanelJson = PullPanelJson.readJSON(value);
                String object_id = pullPanelJson.getObjectID();
                if (!ClientManager.menu.guiComponentMap.containsKey(object_id)) {
                    continue;
                }
                ClientManager.menu.guiComponentMap.get(object_id).upData(pullPanelJson);
            }
        }
    }

    public static void openMenu(String jsonString) {
        if (!ClientManager.open) {
            return;
        }
        if (ClientManager.gui_open) {
            return;
        }
        MenuJson menuJson = MenuJson.readJSON(jsonString);
        menuJson.refreshSize();
        ClientManager.menu = new Menu(new StringTextComponent("PixelGui"), menuJson);
        Minecraft.getInstance().setScreen(ClientManager.menu);
    }
}
